package com.cn.http;

import org.springframework.util.Assert;

import java.util.Iterator;
import java.util.List;

/**
 * sms短信过滤器链默认实现
 * Created by YangJie on 2016/4/20.
 */
public class DefaultSmsFilterChain implements SmsFilterChain {
    private SmsRequest request;

    private  List<SmsFilter> filters;

    private Iterator<SmsFilter> iterator;

    @Override
    public void doFilter(SmsRequest request) {
        Assert.notNull(request, "Request must not be null");

        if (this.request != null) {
            throw new IllegalStateException("This FilterChain has already been called!");
        }

        if (this.iterator == null) {
            this.iterator = this.filters.iterator();
        }

        if (this.iterator.hasNext()) {
            SmsFilter nextFilter = this.iterator.next();
            nextFilter.doFilter(request, this);
        }

        this.request = request;
    }

    public List<SmsFilter> getFilters() {
        return filters;
    }

    public void setFilters(List<SmsFilter> filters) {
        this.filters = filters;
    }
}
